package com.china_goldcard.igas.ui.pages;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.china_goldcard.choosedialog.ChooseDialog;
import com.china_goldcard.choosedialog.ChooseDialogItem;
import com.china_goldcard.choosedialog.ChooseDialogModel;
import com.china_goldcard.gridpagemenu.GridMenuDataAdapter;
import com.china_goldcard.gridpagemenu.GridMenuModel;
import com.china_goldcard.gridpagemenu.GridPageMenu;
import com.china_goldcard.igas.R;
import com.china_goldcard.igas.ui.core.BaseActivity;
import com.china_goldcard.myselectview.MySelectItemModel;
import com.china_goldcard.myselectview.MySelectedView;
import com.china_goldcard.paydialog.OrderItemType;
import com.china_goldcard.paydialog.PayDialog;
import com.china_goldcard.paydialog.PayOrderModel;
import com.china_goldcard.paydialog.PayWayModel;
import com.china_goldcard.topbarview.TopBarView;
import com.orhanobut.logger.Logger;
import com.roughike.bottombar.BottomBar;
import com.zhy.m.permission.MPermissions;
import com.zhy.m.permission.PermissionDenied;
import com.zhy.m.permission.PermissionGrant;
import com.zhy.m.permission.ShowRequestPermissionRationale;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页
 */

public class HomePage extends BaseActivity implements  ChooseDialog.OnClickListener {

    @BindView(R.id.bottomBar)
    BottomBar bottomBar;

    @BindView(R.id.nav_bar_view)
    TopBarView topView;

//    @BindView(R.id.id_flowlayout)
//    TagFlowLayout flowLayout;

//    @BindView(R.id.home_my_gridview)
//    MyGridView myGridView;
//
//    @BindView(R.id.horizontal_menu_view)
    //HorizontalMenuView horizontalMenuView;

    @BindView(R.id.home_menu_view_2)
    GridPageMenu horizontalPageMenuView;

    @BindView(R.id.my_home_select_view)
    MySelectedView mySelectedView;

    @BindView(R.id.pay_dialog_1)
    PayDialog payDialog;

    //
    private String[] mVals = new String[]{"20", "50", "100", "120", "300", "500"};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        List<GridMenuModel> list1 = new ArrayList<>();
        GridMenuModel model1 = new GridMenuModel();
        model1.setIcon(R.mipmap.ic_favorites);
        model1.setTitle("我的收藏");
        model1.setPlaceHolder(R.mipmap.ic_favorites);
        model1.setFromNet(true);
        model1.setImageUrl("http://img.hb.aicdn.com/d2024a8a998c8d3e4ba842e40223c23dfe1026c8bbf3-OudiPA_fw580");
        GridMenuModel model2 = new GridMenuModel();
        model2.setIcon(R.mipmap.ic_friends);
        model2.setTitle("我的朋友");
        GridMenuModel model3 = new GridMenuModel();
        model3.setIcon(R.mipmap.ic_nearby);
        model3.setTitle("附近的人");
        GridMenuModel model4 = new GridMenuModel();
        model4.setIcon(R.mipmap.ic_recents);
        model4.setTitle("我的时间4");
        GridMenuModel model5 = new GridMenuModel();
        model5.setIcon(R.mipmap.ic_recents);
        model5.setTitle("我的时间5");
        GridMenuModel model6 = new GridMenuModel();
        model6.setIcon(R.mipmap.ic_recents);
        model6.setTitle("我的时间6");
        GridMenuModel model7 = new GridMenuModel();
        model7.setIcon(R.mipmap.ic_recents);
        model7.setTitle("我的时间7");
        GridMenuModel model8 = new GridMenuModel();
        model8.setIcon(R.mipmap.ic_recents);
        model8.setTitle("我的时间8");
        GridMenuModel model9 = new GridMenuModel();
        model9.setIcon(R.mipmap.ic_recents);
        model9.setTitle("我的时间9");
        GridMenuModel model10 = new GridMenuModel();
        model10.setIcon(R.mipmap.ic_recents);
        model10.setTitle("我的时间10");
        GridMenuModel model11 = new GridMenuModel();
        model11.setIcon(R.mipmap.ic_recents);
        model11.setTitle("我的时间11");
        GridMenuModel model12 = new GridMenuModel();
        model12.setIcon(R.mipmap.ic_recents);
        model12.setTitle("我的时间12");
        list1.add(model1);
        list1.add(model2);
        list1.add(model3);
        list1.add(model4);
        list1.add(model5);
        list1.add(model6);
        list1.add(model7);
        list1.add(model8);
        list1.add(model9);
        list1.add(model10);
        list1.add(model11);
        list1.add(model12);
        horizontalPageMenuView.loadDataWithDataAdapter(list1, getSupportFragmentManager(), new GridMenuDataAdapter<GridMenuModel>() {
            @Override
            public View getView(GridMenuModel item, View convertView, ViewGroup parent) {
                MyViewHolder holder = null;
                if(convertView == null){
                    convertView = LayoutInflater.from(HomePage.this).inflate(R.layout.grid_item,parent,false);
                    holder = new MyViewHolder();
                    holder.textView = (TextView) convertView.findViewById(R.id.my_grid_item_tv);
                    convertView.setTag(holder);
                }else{
                    holder = (MyViewHolder) convertView.getTag();
                }
                holder.textView.setText(item.getTitle());
                return convertView;
            }
        });
        horizontalPageMenuView.setMenuItemClickListener(new GridPageMenu.OnMenuItemClickListener() {
            @Override
            public void onItemClicked(GridMenuModel item) {

            }

            @Override
            public void onItemLongClicked(GridMenuModel item) {
                Logger.d("onItemLongClicked item title is " + item.getTitle());
            }
        });

//        HorizontalMenuViewModel menu1 = new HorizontalMenuViewModel();
//        menu1.setIcon(getResources().getDrawable(R.mipmap.ic_favorites));
//        menu1.setTitle("我的收藏");
//        menu1.setPlaceHolder(getResources().getDrawable(R.mipmap.ic_favorites));
//        menu1.setFromNet(true);
//        menu1.setImageUrl("http://img.hb.aicdn.com/d2024a8a998c8d3e4ba842e40223c23dfe1026c8bbf3-OudiPA_fw580");
//        HorizontalMenuViewModel menu2 = new HorizontalMenuViewModel();
//        menu2.setIcon(getResources().getDrawable(R.mipmap.ic_friends));
//        menu2.setTitle("我的朋友");
//        HorizontalMenuViewModel menu3 = new HorizontalMenuViewModel();
//        menu3.setIcon(getResources().getDrawable(R.mipmap.ic_nearby));
//        menu3.setTitle("附近的人");
//        HorizontalMenuViewModel menu4 = new HorizontalMenuViewModel();
//        menu4.setIcon(getResources().getDrawable(R.mipmap.ic_recents));
//        menu4.setTitle("我的时间9");
//        List<HorizontalMenuViewModel> models = new ArrayList<>();
//        models.add(menu1);
//        models.add(menu2);
//        models.add(menu3);
//        models.add(menu4);
//        horizontalMenuView.loadData(models);
//        horizontalMenuView.setOnItemClickListener(new HorizontalMenuView.OnItemClickListener() {
//            @Override
//            public void onItemClick(HorizontalMenuViewModel model, int position) {
//                if (!MPermissions.shouldShowRequestPermissionRationale(HomePage.this,Manifest.permission.CAMERA,124)){
//                    MPermissions.requestPermissions(HomePage.this,123, Manifest.permission.CAMERA);
//                }
//            }
//
//            @Override
//            public void onItemLongClick(HorizontalMenuViewModel model, int position) {
//
//            }
//        });

        topView.setClickListener(new TopBarView.NavBtnOnClickListener() {
            @Override
            public void leftBtnClicked(View view) {
                ChooseDialogModel model = new ChooseDialogModel();
                model.setItemW(250);
                List<ChooseDialogItem> list = new ArrayList<ChooseDialogItem>();
                ChooseDialogItem model1 = new ChooseDialogItem();
                model1.setSelected(true);
                model1.setTitle("liyansheng");
                ChooseDialogItem model2 = new ChooseDialogItem();
                model2.setSelected(false);
                model2.setTitle("liyansheng");
                ChooseDialogItem model3 = new ChooseDialogItem();
                model3.setSelected(false);
                model3.setTitle("liyansheng");
                ChooseDialogItem model4 = new ChooseDialogItem();
                model4.setSelected(false);
                model4.setTitle("liyansheng4");
                list.add(model1);
                list.add(model2);
                list.add(model3);
                list.add(model4);
                model.setDataList(list);
                model.setTitle("请选择姓名");
                model.setBtn("确定>>");
                model.setSelectIcon(R.mipmap.selected);
                ChooseDialog.newInstance(HomePage.this,model).show(getSupportFragmentManager(),"choose_dialog");
            }

            @Override
            public void rightBtnClicked(View view) {
                mySelectedView.toggle();
            }

            @Override
            public void centerBtnClicked(View view) {

            }
        });

        //
        MySelectItemModel itemModel = new MySelectItemModel("李焱省",false);
        MySelectItemModel itemModel2 = new MySelectItemModel("李焱省1",false);
        MySelectItemModel itemModel3 = new MySelectItemModel("李焱省2",false);
        MySelectItemModel itemModel4 = new MySelectItemModel("李焱省3",false);
        MySelectItemModel itemModel5 = new MySelectItemModel("李焱省4",false);
        MySelectItemModel itemModel6 = new MySelectItemModel("李焱省4",false);
        MySelectItemModel itemModel7 = new MySelectItemModel("李焱省4",false);
        MySelectItemModel itemModel8 = new MySelectItemModel("李焱省4",false);
        MySelectItemModel itemModel9 = new MySelectItemModel("李焱省4",false);
        itemModel8.setSelected(true);
        List<MySelectItemModel> dataList = new ArrayList<>();
        dataList.add(itemModel);
        dataList.add(itemModel2);
        dataList.add(itemModel3);
        dataList.add(itemModel4);
        dataList.add(itemModel5);
        dataList.add(itemModel6);
        dataList.add(itemModel7);
        dataList.add(itemModel8);
        dataList.add(itemModel9);
        mySelectedView.loadData(this,dataList);

        List<PayOrderModel> list = new ArrayList<>();

        PayOrderModel info1 = new PayOrderModel();
        info1.setOrderItemValue("物联网充值");
        info1.setType(OrderItemType.OrderInfo);

        PayOrderModel info2 = new PayOrderModel();
        info2.setOrderItemValue("微信支付");
        info2.setType(OrderItemType.PayWayInfo);

        PayOrderModel info3 = new PayOrderModel();
        info3.setOrderItemValue("100元");
        info3.setType(OrderItemType.MoneyInfo);

        list.add(info1);
        list.add(info2);
        list.add(info3);
        payDialog.reloadOrderInfo(list);


        List<PayWayModel> list2 = new ArrayList<>();
        PayWayModel pay1 = new PayWayModel();
        pay1.setPayIcon(R.mipmap.alipay_pay_icon);
        pay1.setPaywayName("支付宝");
        pay1.setSelected(true);
        PayWayModel pay2 = new PayWayModel();
        pay2.setPayIcon(R.mipmap.webchat_pay_icon);
        pay2.setPaywayName("微信支付");
        pay2.setSelected(false);
        PayWayModel pay3 = new PayWayModel();
        pay3.setPayIcon(R.mipmap.bankcode_icon_other);
        pay3.setPaywayName("银行卡支付");
        pay3.setSelected(false);
        list2.add(pay1);
        list2.add(pay2);
        list2.add(pay3);
        payDialog.reloadPayWayInfo(list2);
    }

    @ShowRequestPermissionRationale(124)
    public void showExplain(){
        Toast.makeText(this, "showExplain", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MPermissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @PermissionGrant(123)
    public void requestSdcardSuccess()
    {
        Toast.makeText(this, "GRANT ACCESS SDCARD!", Toast.LENGTH_SHORT).show();
    }

    @PermissionDenied(123)
    public void requestSdcardFailed()
    {
        Toast.makeText(this, "DENY ACCESS SDCARD!", Toast.LENGTH_SHORT).show();
    }

    class MyViewHolder{
        TextView textView;
    }

    @Override
    protected boolean supportSlideClose() {
        return true;
    }

    @Override
    protected boolean hasNavBar() {
        return true;
    }


    @Override
    protected Integer layoutResource() {
        return R.layout.home_page;
    }

    @Override
    public void onItemClick(ChooseDialog dialog, ChooseDialogItem item) {
        dialog.dismiss();
        Logger.d("item = " + item);
    }

    @Override
    public void onBtnClick(ChooseDialog dialog, ChooseDialogItem item) {
        dialog.dismiss();
        Logger.d("item = " + item);
    }
}
